package controller;

import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
import ui.Main;
import common.Course;
import common.Message;
import common.MessageType;
import common.User;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.URL;
import java.util.ResourceBundle;

public class NewController implements Initializable {
    private static User user=Main.userClientService.getUser();
    private static Socket socket=Main.userClientService.getSocket();
    private ObjectOutputStream oos;
    @FXML
    private TextField name;
    @FXML
    private TextArea description;
    @FXML
    private Label error;
    public static Course course;
    public void initialize(URL location, ResourceBundle resources){
        description.setWrapText(true); // 设置文本自动换行
    }
    @FXML
    public void ok(){
        if(name.getText().equals(""))
        {
            error.setText("课程名为空");
            return;
        }
        try {
            ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
            Message ms=new Message();
            ms.setState(MessageType.new_Class);//发送请求
            course=new Course(name.getText(),user.getId(),user.getName(),description.getText(),1);
            oos.writeObject(ms);
            oos.writeObject(course);
        } catch (Exception e) {
            System.out.println("新建课程失败");
        }finally {
            Stage stage=(Stage) name.getScene().getWindow();
            stage.close();
        }
    }
}
